﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InventoryMgtServiceRef;
    using WMS6.Web.UserControls;

    public class ReplenishementExeList : BasePage
    {
        protected Button btnConfirm;
        protected Button btnExport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5252;
        protected GridView gvReplenishementExeList;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDatareplenishmentQueryEntityList(ReplenishmentQueryEntity ReplenishmentQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(ReplenishmentQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = InventoryMgtServiceClientProxy.GetInstance().GetReplenishmentByQueryList(ReplenishmentQueryEntity);
            base.BindData2GridView(this.gvReplenishementExeList, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvReplenishementExeList, "REPLENISHMENT_ID");
            List<string> status = base.GetSelectedIDsInGridView(this.gvReplenishementExeList, "STATUS");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else
            {
                try
                {
                    for (int i = 0; status.Count > i; i++)
                    {
                        if (status[i].ToUpper() == "COMPLETED")
                        {
                            this.lblMsg.Text = "只能对挂起的行项进行确认!";
                            return;
                        }
                    }
                    string strtemp = InventoryMgtServiceClientProxy.GetInstance().ReplenishmentConfirm(selectedID);
                    this.BindDatareplenishmentQueryEntityList(this.GetReplenishmentQueryEntity());
                    this.lblMsg.Text = strtemp + "确认成功!";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity RequestQueryEntity = this.GetReplenishmentQueryEntity();
            RequestQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = RequestQueryEntity.CurrentPage;
            RequestQueryEntity.CurrentPage = "0";
            this.gvReplenishementExeList.Columns[1].Visible = false;
            this.gvReplenishementExeList.AllowSorting = false;
            this.BindDatareplenishmentQueryEntityList(RequestQueryEntity);
            this.hdnActionID.Value = "";
            this.gvReplenishementExeList.ShowFooter = false;
            RequestQueryEntity.IsGetAll = false;
            RequestQueryEntity.CurrentPage = currentPageIndex;
            this.SetReplenishmentQueryEntity(RequestQueryEntity);
            base.Export2Excel("Replenishment.xls", this.gvReplenishementExeList);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
            replenishmentQueryEntity.CurrentPage = "0";
            this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
            replenishmentQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
            replenishmentQueryEntity.OwnerID = "";
            replenishmentQueryEntity.SkuID = "";
            replenishmentQueryEntity.Lot = "";
            replenishmentQueryEntity.FromLoc = "";
            replenishmentQueryEntity.ToLoc = "";
            this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
            if (int.Parse(replenishmentQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                replenishmentQueryEntity.CurrentPage = (int.Parse(replenishmentQueryEntity.CurrentPage) + 1).ToString();
                this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
            if (int.Parse(replenishmentQueryEntity.CurrentPage) > 0)
            {
                replenishmentQueryEntity.CurrentPage = (int.Parse(replenishmentQueryEntity.CurrentPage) - 1).ToString();
                this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
            }
        }

        private ReplenishmentQueryEntity GetReplenishmentQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as ReplenishmentQueryEntity);
            }
            return (base.PageQueryEntitySession as ReplenishmentQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
                replenishmentQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
            }
        }

        protected void gvReplenishementExeList_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5252", this.gvReplenishementExeList);
        }

        protected void gvReplenishementExeList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvReplenishementExeList_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ReplenishmentQueryEntity replenishmentQueryEntity = this.GetReplenishmentQueryEntity();
                replenishmentQueryEntity.SortField = e.SortExpression;
                if (replenishmentQueryEntity.SortDirection == "ASC")
                {
                    replenishmentQueryEntity.SortDirection = "DESC";
                }
                else if (replenishmentQueryEntity.SortDirection == "DESC")
                {
                    replenishmentQueryEntity.SortDirection = "ASC";
                }
                this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ReplenishmentQueryEntity replenishmentQueryEntity = new ReplenishmentQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    replenishmentQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    replenishmentQueryEntity.SkuID = this.PreviousPage.SkuID;
                    replenishmentQueryEntity.Lot = this.PreviousPage.Lot;
                    replenishmentQueryEntity.FromLoc = this.PreviousPage.FromLoc;
                    replenishmentQueryEntity.ToLoc = this.PreviousPage.ToLoc;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            replenishmentQueryEntity.IsGetAll = false;
            replenishmentQueryEntity.CurrentPage = "0";
            replenishmentQueryEntity.PageSize = this.gvReplenishementExeList.PageSize.ToString();
            replenishmentQueryEntity.SortField = "R.ROWID";
            replenishmentQueryEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvReplenishementExeList.PageSize;
            this.SetReplenishmentQueryEntity(replenishmentQueryEntity);
            this.BindDatareplenishmentQueryEntityList(replenishmentQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                base.CreateInitGridviewSQL("5252", this.gvReplenishementExeList);
            }
        }

        private void SetReplenishmentQueryEntity(ReplenishmentQueryEntity ReplenishmentQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = ReplenishmentQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = ReplenishmentQueryEntity;
            }
        }

        public ReplenishementExeQuery PreviousPage
        {
            get
            {
                return (ReplenishementExeQuery) base.PreviousPage;
            }
        }
    }
}

